package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.cache.RedisKeyPrefix;
import com.juyu.miliao.api.domain.GiftSpend;
import com.juyu.miliao.common.constant.GiftWallRedisKey;
import com.juyu.miliao.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


/**
 * 礼物墙Redis
 *
 * @author xuanqicheng
 * @create 2019-18-14:32
 */
@Repository
public class GiftWallRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public Set<String> getGiftByMax() {

        String time = LocalDate.now().toString();

        int max = getMaxGift(time);

        String key = GiftWallRedisKey.GIFT_WALL_MAX_ZSET + time;

        Set<String> stringSet = stringRedisTemplate.opsForZSet().rangeByScore(key, max, max);

        return stringSet;
    }


    /**
     * 获取最大金币数
     *
     * @param time
     * @return
     */
    private int getMaxGift(String time) {

        String temp = stringRedisTemplate.opsForValue().get(GiftWallRedisKey.GIFT_MAX_INFO + time);

        if (!StringUtils.isEmpty(temp)) {
            int max = Integer.parseInt(temp);
            return max;

        } else {

            return 0;

        }

    }


    public Set<String> getGiftInfo(String key,int startNum, int endNumber) {

        Set<String> obj = stringRedisTemplate.opsForZSet().reverseRange(key, startNum, endNumber);
        return obj;
    }

    public boolean checkLikeInfo(long userId, int sendId) {
        String key = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_CHECK + sendId;
        boolean bl = stringRedisTemplate.opsForSet().isMember(key, Long.toString(userId));
        return bl;
    }

    public void setLikeInfo(long userId, int sendId) {
        String key = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_BY_SENDID + sendId;


        stringRedisTemplate.opsForZSet().add(key, Long.toString(userId), System.currentTimeMillis());

        stringRedisTemplate.expire(key, 15, TimeUnit.DAYS);

        String checkKey = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_CHECK + sendId;

        stringRedisTemplate.opsForSet().add(checkKey, Long.toString(userId));

        stringRedisTemplate.expire(checkKey, 15, TimeUnit.DAYS);

    }

    public void removeLikeInfo(long userId, int sendId) {

        String key = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_BY_SENDID + sendId;

        stringRedisTemplate.opsForZSet().remove(key, Long.toString(userId));


        stringRedisTemplate.expire(key, 15, TimeUnit.DAYS);

        String checkKey = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_CHECK + sendId;

        stringRedisTemplate.opsForSet().remove(checkKey, Long.toString(userId));

        stringRedisTemplate.expire(checkKey, 15, TimeUnit.DAYS);
    }

    public Set<String> getLikeInfo(int sendId) {

        String key = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_BY_SENDID + sendId;

        Set<String> likeSet = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 5);

        return likeSet;
    }

    public long getLikeSizeInfo(int sendId) {
        String key = GiftWallRedisKey.GIFT_WALL_LIKE_INFO_BY_SENDID + sendId;
        long size = stringRedisTemplate.opsForZSet().size(key);
        return size;
    }

}
